***********************************************
* Authors: Henrik Andersson & Sirus Dehdari
* This do-file generates Figure D1 in the online Appendix
*************************

clear all

cd "D:\SCB_ConPol\Stata"


///can do this for any year, 

foreach y of numlist 2006(1)2013 {

use "AF\Arbetsformedlingen"

///generate variable for year and month of getting notice
tostring anstupphor_dat, replace
gen termination_year = substr(anstupphor_dat, 1, 4) 
destring termination_year, replace

gen termination_month = substr(anstupphor_dat, 6, 2) 
destring termination_month, replace

tostring inkom_dat, replace
gen notice_year = substr(inkom_dat, 1, 4) 
destring notice_year, replace

gen notice_month = substr(inkom_dat, 6, 2) 
destring notice_month, replace

drop inkom_dat anstupphor_dat

gen year = notice_year


keep if year==`y'
drop if LopNr==.
duplicates drop LopNr, force
merge 1:m LopNr using "LISA\LISA_`y'"

///some clensing: drop those without a workplace, and SSYK-code, 
*drop _merge, drop a few duplicates (like 2)
drop if ArbstId==.
drop if SSYK3=="***"
drop _merge
duplicates drop LopNr, force

///use first 2 digits in SSYK and merge on RTI data
gen SSYK2 = substr(SSYK3, 1,2)

merge m:1 SSYK2 using "E:\ProjData\IntegrationSD\raw\RTI_and_offshoring.dta" 
drop _merge
drop if stdRTI ==.

///Now we want to see the relationship between getting a notice and RTI-score 

gen notice =1 if notice_year==`y'
gen workers =1

collapse (count) workers notice (mean) AntalSys, by(stdRTI)

gen share_notice = notice/workers

save "E:\ProjData\IntegrationSD\temp\RTI`y'notices", replace

}

*Create panel 

use "E:\ProjData\IntegrationSD\temp\RTI2006notices"
gen year=2006
foreach y of numlist 2007(1)2013 {
append using "E:\ProjData\IntegrationSD\temp\RTI`y'notices" 
replace year=`y' if year==.
}

save "E:\ProjData\IntegrationSD\use\RTInoticespanel", replace
foreach y of numlist 2006(1)2013 {
erase "E:\ProjData\IntegrationSD\temp\RTI`y'notices.dta" 
}

///Do one for all years
collapse (mean) workers notice AntalSys share_notice, by(stdRTI)

*only one worker in one of the groups - meningless
drop if workers==1
corr share_notice stdRTI

reg share_notice stdRTI workers, robust 
esttab using "C:\Userdata\Shared\Output\IntegrationSD\RTIsharenotice.tex", ///
se noconstant float compress label scalars(model N) ar2  /// 
keep(stdRTI) title("Effect of RTI on notices") /// 
nonotes ///
addnotes("Standard errors clustered on precinct level" "\sym{*} \(p<0.05\), \sym{**} \(p<0.01\), \sym{***} \(p<0.001\)") ///
replace 

scatter share_notice stdRTI, ///
graphregion(color(white)) xtitle("RTI standardized score for ISCO 2-digit occupations") ///
ytitle("Mean share of notified 2006-2013") legend(off) 
graph export "C:\Userdata\Shared\Output\IntegrationSD\scatterRTI.pdf", replace
graph export "C:\Users\henrik\Dropbox\IntregrationSD\Output\Figures\scatterRTI.pdf", replace

gen lnsys = ln(AntalSys)
scatter lnsys stdRTI, ///
graphregion(color(white)) xtitle("RTI standardized score for ISCO 2-digit occupations") ///
ytitle("log of Mean number of employed at workplace") legend(off) 
graph export "C:\Userdata\Shared\Output\IntegrationSD\scatterRTI_wp.pdf", replace
graph export "C:\Users\henrik\Dropbox\IntregrationSD\Output\Figures\scatterRTI_wp.pdf", replace

///Do one excluding 2008 and 2009
clear
use "E:\ProjData\IntegrationSD\use\RTInoticespanel"

drop if year==2008 | year==2009
collapse (mean) workers notice share_notice, by(stdRTI)

*only one worker in one of the groups - meningless
drop if workers==1
corr share_notice stdRTI

reg share_notice stdRTI workers, robust 
esttab using "C:\Userdata\Shared\Output\IntegrationSD\RTIsharenotice_noFC.tex", ///
se noconstant float compress label scalars(model N) ar2  /// 
keep(stdRTI) title("Effect of RTI on notices") /// 
nonotes ///
addnotes("Standard errors clustered on precinct level" "\sym{*} \(p<0.05\), \sym{**} \(p<0.01\), \sym{***} \(p<0.001\)") ///
replace 

scatter share_notice stdRTI, ///
graphregion(color(white)) xtitle("RTI standardized score"  "ISCO 2-digit occupations") ///
ytitle("Mean share of notified 2006-2013" "(Excluding 2008-2009)") legend(off) 
graph export "C:\Userdata\Shared\Output\IntegrationSD\scatterRTI_noFC.pdf", replace
graph export "C:\Users\henrik\Dropbox\IntregrationSD\Output\Figures\scatterRTI_noFC.pdf", replace

